package com.pactera.madp.cp.service.sync.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.cp.api.dto.sync.MaMarketingLeadDTO;
import com.pactera.madp.cp.api.entity.sync.MaMarketingLeadEntity;
import com.pactera.madp.cp.mapper.sync.MaMarketingLeadMapper;
import com.pactera.madp.cp.service.sync.MaMarketingLeadService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @title 经理视图 -> 营销线索 业务接口实现
 * @date 2022-05-13
 * @author Deming.Chang
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MaMarketingLeadServiceImpl extends ServiceImpl<MaMarketingLeadMapper, MaMarketingLeadEntity> implements MaMarketingLeadService {

    @Override
    public int summaryAndSave(MaMarketingLeadDTO dto) {
        return baseMapper.summaryAndSaveByRecommend(dto);
    }

    @Override
    public int removeHisData(String maDate, int offset) {
        String date = DateUtil.format(DateUtil.offsetDay(DateUtil.parse(maDate), offset), DatePattern.NORM_DATE_PATTERN);
        return baseMapper.delete(Wrappers.<MaMarketingLeadEntity>query().lambda().le(MaMarketingLeadEntity::getMaDate, date));
    }

    @Override
    public Integer getNewestCount(String wxUserId) {
        return baseMapper.selectCount(new LambdaQueryWrapper<MaMarketingLeadEntity>()
                .eq(MaMarketingLeadEntity::getMaDate,DateUtil.format(DateUtil.yesterday(),"yyyy-MM-dd"))
                .eq(MaMarketingLeadEntity::getRecommendId,wxUserId));
    }

}

